package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxda;
import cn.stylefeng.guns.sys.modular.system.entity.BtyZxHuopinFenlei;
import cn.stylefeng.guns.sys.modular.system.entity.ZxHuopin;
import cn.stylefeng.guns.sys.modular.system.entity.ZxKemu;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxHuopinParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyBxdaResult;
import cn.stylefeng.guns.sys.modular.system.model.result.ZxHuopinResult;
import cn.stylefeng.guns.sys.modular.system.service.*;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


/**
 * 装修货品表控制器
 *
 * @author jiangge
 * @Date 2020-11-02 16:51:34
 */
@Controller
@RequestMapping("/dgdzxHuopin")
public class DgdZxHuopinController extends BaseController {

    private Long pinpai = 1417361607353536514L;

    private String PREFIX = "/modular/system/dgdzxHuopin";

    @Autowired
    private ZxHuopinService zxHuopinService;
    @Autowired
    private ZxKemuService zxKemuService;
    @Resource
    private BtyBxdaService btyBxdaService;
    @Resource
    private ZxChengbenMingxiService zxChengbenMingxiService;
    @Resource
    private BtyZxHuopinFenleiService fenleiService;
    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/zxHuopin.html";
    }

    /**
     * 跳转到供货商界面
     *
     * @author zwf
     * @Date 2021-01-16
     */
    @RequestMapping("ghs")
    public String index_ghs(Model model) {
        //当前用户信息
        String name =  LoginContextHolder.getContext().getUser().getName();
        String tel =  LoginContextHolder.getContext().getUser().getAccount();
        BtyBxdaResult bxda = btyBxdaService.getByTel("半天妖",tel);
        if(bxda ==null){
            BtyBxda mtp = new BtyBxda();
            mtp.setXm(name);
            mtp.setTel(tel);
            mtp.setDm("半天妖");
            btyBxdaService.add(mtp);

            bxda = new BtyBxdaResult();
            bxda.setId(String.valueOf(mtp.getId()));
            bxda.setTel(tel);
            bxda.setXm(name);
        }
        model.addAttribute("bxda",bxda);
        return PREFIX + "/zxHuopin_ghs.html";
    }

    /**
     * 新增页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/zxHuopin_add.html";
    }

    /**
     * 设置科目
     *
     * @author zwf
     * @Date 2021-4-25
     */
    @RequestMapping("/setkemu")
    public String setkemu() {
        return PREFIX + "/zxHuopin_setKemu.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/zxHuopin_edit.html";
    }

    @RequestMapping("/editGsh")
    public String editGsh() {
        return PREFIX + "/zxHuopin_editGsh.html";
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ZxHuopinParam zxHuopinParam) {
        zxHuopinParam.setPinpai(pinpai);
        //根据科目id+名称 判断是否存在
        ZxHuopinParam isExist=new ZxHuopinParam();
        isExist.setBtyZxKemuId(zxHuopinParam.getBtyZxKemuId());
        isExist.setName(zxHuopinParam.getName());
        ZxHuopinResult bySpec = this.zxHuopinService.findBySpec(isExist);
        if(ToolUtil.isNotEmpty(bySpec)){
            return ResponseData.error(1,"此科目下该货品已存在!");
        }
        ZxKemu km = zxKemuService.getById(zxHuopinParam.getBtyZxKemuId());
        if(km==null || (km!=null&&Integer.valueOf(km.getLevel())!=2)){
            return ResponseData.error(1,"必须选择二级科目!");
        }
        this.zxHuopinService.add(zxHuopinParam);
        return ResponseData.success();
    }
    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/addItemGhs")
    @ResponseBody
    public ResponseData addItemGhs(ZxHuopinParam zxHuopinParam) {
        zxHuopinParam.setPinpai(pinpai);
        //根据科目id+名称 判断是否存在
        this.zxHuopinService.add(zxHuopinParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ZxHuopinParam zxHuopinParam) {
        zxHuopinParam.setPinpai(pinpai);
        //根据科目id+名称 判断是否存在
        ZxHuopinParam isExist=new ZxHuopinParam();
        isExist.setBtyZxKemuId(zxHuopinParam.getBtyZxKemuId());
        isExist.setName(zxHuopinParam.getName());
        ZxHuopinResult bySpec = this.zxHuopinService.findBySpec(isExist);
        if(ToolUtil.isNotEmpty(bySpec) && !bySpec.getBtyZxHuopinId().equals(zxHuopinParam.getBtyZxHuopinId())){
            return ResponseData.error(1,"此科目下该货品已存在!");
        }
        ZxKemu km = zxKemuService.getById(zxHuopinParam.getBtyZxKemuId());
        if(Integer.valueOf(km.getLevel())!=2){
            return ResponseData.error(1,"必须选择二级科目!");
        }
        this.zxHuopinService.update(zxHuopinParam);
        //同步货品
        zxChengbenMingxiService.updateKemuByHuopinId(zxHuopinParam.getBtyZxHuopinId());
        return ResponseData.success();
    }

    /**
     * 批量更新科目
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/editItemOfKemu")
    @ResponseBody
    public ResponseData editItemOfKemu(ZxHuopinParam zxHuopinParam) {
        zxHuopinParam.setPinpai(pinpai);
        String zxHuopinIds = zxHuopinParam.getBtyZxHuopinIds();
        String[] zxHuopinIdsArr=  zxHuopinIds.split(",");
        for(String btyZxHuopinId:zxHuopinIdsArr){
            this.zxHuopinService.update(btyZxHuopinId,zxHuopinParam.getBtyZxKemuId(),zxHuopinParam.getBtyZxKemuIdView());
        }
        return ResponseData.success();
    }

    @RequestMapping("/setJgc")
    @ResponseBody
    public ResponseData setJgc(ZxHuopinParam zxHuopinParam) {
        zxHuopinParam.setPinpai(pinpai);
        String zxHuopinIds = zxHuopinParam.getBtyZxHuopinIds();
        String[] zxHuopinIdsArr=  zxHuopinIds.split(",");
        for(String btyZxHuopinId:zxHuopinIdsArr){
            this.zxHuopinService.updateJgc(btyZxHuopinId,zxHuopinParam.getIsJgc());
        }
        return ResponseData.success();
    }


    @RequestMapping("/saveGhs")
    @ResponseBody
    public ResponseData saveGhs(ZxHuopinParam zxHuopinParam) {
        zxHuopinParam.setPinpai(pinpai);
        this.zxHuopinService.updateGhs(zxHuopinParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/editItemGhs")
    @ResponseBody
    public ResponseData editItemGhs(ZxHuopinParam zxHuopinParam) {
        zxHuopinParam.setPinpai(pinpai);
        //根据科目id+名称 判断是否存在
        this.zxHuopinService.update(zxHuopinParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ZxHuopinParam zxHuopinParam) {
        ResponseData data = this.zxHuopinService.delete(zxHuopinParam);
        return data;
    }

    /**
     * 查看详情接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ZxHuopinParam zxHuopinParam) {
        ZxHuopin detail = this.zxHuopinService.getById(zxHuopinParam.getBtyZxHuopinId());
        if (ToolUtil.isNotEmpty(detail.getZxHuopinFenleiId())&&detail.getZxHuopinFenleiId() != 0L && detail != null) {
            BtyZxHuopinFenlei parentDetail = fenleiService.getById(detail.getZxHuopinFenleiId());
            if (parentDetail != null) {
                detail.setPName(parentDetail.getName());
            }
        }
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ZxHuopinParam zxHuopinParam) {
        zxHuopinParam.setPinpai(pinpai);
        return this.zxHuopinService.findPageBySpec(zxHuopinParam);
    }

    @ResponseBody
    @RequestMapping("/ghsList")
    public List<ZxHuopinResult> ghsList(ZxHuopinParam zxHuopinParam) {
        zxHuopinParam.setPinpai(pinpai);
        return this.zxHuopinService.ghsPageList(zxHuopinParam);
    }
    /**
     * 根据门店查询货品列表
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/listHuopinSelect")
    public ResponseData listHuopinSelect() {
        return ResponseData.success(this.zxHuopinService.listHuopinSelect());
    }

}


